#include <iostream>
#include <ctime>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>

typedef void(*task_t)();
#define TaskNum 3
void Print()
{
    std::cout<<" I am print task "<<std::endl;
}
void DownLoad()
{
    std::cout<<" I am downLoad task "<<std::endl;
}
void Flush()
{
    std::cout<<" I am flush task "<<std::endl;
}

task_t tasks[TaskNum];

void ExcuteTask(int command)
{
    if(command<0||command>2)
    return;
    tasks[command]();
}
void LoadTask()
{
    srand((unsigned int)time(NULL)^getpid()^0x7778);
    tasks[0]=Print;
    tasks[1]=DownLoad;
    tasks[2]=Flush;
}
int SelectTask()
{
    return rand()%TaskNum;
}

void Work()
{
    while (true)
    {
        int command = 0;
        int n = read(0, &command, sizeof(command));
        if (n == sizeof(int))
        {
            std::cout << " pid is : " << getpid() << " handler task " << std::endl;
            ExcuteTask(command);
        }
        else if (n == 0)
        {
            std::cout << " sub process : " << getpid() << " quit " << std::endl;
            break;
        }
    }
}

